package com.moyuxi.sound_of_the_forest.ui.pages.musicClassify

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.moyuxi.sound_of_the_forest.entity.LocalMusicBean
import com.moyuxi.sound_of_the_forest.ui.compose.MyTopAppBarCompose
import com.moyuxi.sound_of_the_forest.ui.compose.SongListCompose
import com.moyuxi.sound_of_the_forest.viewModel.MainViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchMusicFrame(
    mainVM: MainViewModel,
    musicClassifyNC: NavHostController,
    playingAllClick: (List<LocalMusicBean>) -> Unit,
    playingInBean: (LocalMusicBean) -> Unit,
    addToPlayingList: (LocalMusicBean) -> Unit,
    removeFromDataList: (LocalMusicBean) -> Unit
) {
    var text by remember { mutableStateOf("") }
    mainVM.searchSong(text)
    Column {
        MyTopAppBarCompose(visible = false) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .background(MaterialTheme.colorScheme.primaryContainer)
                    .padding(3.dp), verticalAlignment = Alignment.CenterVertically
            ) {
                TextField(
                    value = text,
                    onValueChange = {
                        text = it
                        mainVM.searchSong(text)
                    },
                    modifier = Modifier
                        .padding(3.dp)
                        .weight(1f),
                    shape = CircleShape,
                    singleLine = true,
                    leadingIcon = @Composable { Icon(Icons.Filled.Search, "搜索") },
                    trailingIcon = @Composable {
                        Icon(Icons.Filled.Clear,
                            "clear",
                            Modifier
                                .border(1.dp, Color.Black, CircleShape)
                                .clickable {
                                    text = ""
                                    mainVM.searchSong(text)
                                })
                    },
                    placeholder = @Composable { Text("o((>ω< ))o") },
                    colors = TextFieldDefaults.textFieldColors(
                        focusedIndicatorColor = Color.Transparent,
                        unfocusedIndicatorColor = Color.Transparent,
                        disabledIndicatorColor = Color.Transparent,
                        errorIndicatorColor = Color.Transparent
                    )
                )
                Text(text = "取消",
                    fontSize = 20.sp,
                    modifier = Modifier.clickable { musicClassifyNC.popBackStack() })
            }
        }
        mainVM.targetSong.value?.let {
            SongListCompose(
                it, playingAllClick, playingInBean, addToPlayingList, removeFromDataList
            )
        } ?: Text(text = "(´。＿。｀)")
    }

}

